package com.eam.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.eam.bean.Asset;
import com.eam.dao.AssetDAO;


public class AssetDAOImpl extends HibernateDaoSupport implements AssetDAO {
	private static final Log log = LogFactory.getLog(AssetDAOImpl.class);
	// property constants
	public static final String NAME = "name";
	public static final String NUM = "num";
	public static final String VALUE = "value";
	public static final String STATUS = "status";
	public static final String SPEC = "spec";
	public static final String DEPARTMENT_ID = "departmentId";

	protected void initDao() {
		// do nothing
	}

	public boolean save(Asset transientInstance) {
		log.debug("saving Asset instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			return false;
		}
	}

	public boolean delete(Asset persistentInstance) {
		log.debug("deleting Asset instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			return false;
		}
	}
	
	public boolean update(Asset persistentInstance){
		log.debug("update Asset instance");
		try{
			getHibernateTemplate().update(persistentInstance);
			log.debug("update successful");
			return true;
		}catch (RuntimeException re) {
			log.error("update failed", re);
			return false;
		}
	}

	public Asset findById(java.lang.Integer id) {
		log.debug("getting Asset instance with id: " + id);
		try {
			Asset instance = (Asset) getHibernateTemplate().get(
					"com.eam.dao.Asset", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Asset instance) {
		log.debug("finding Asset instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Asset instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Asset as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByNum(Object num) {
		return findByProperty(NUM, num);
	}

	public List findByValue(Object value) {
		return findByProperty(VALUE, value);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findBySpec(Object spec) {
		return findByProperty(SPEC, spec);
	}

	public List findByDepartmentId(Object departmentId) {
		return findByProperty(DEPARTMENT_ID, departmentId);
	}

	public List findAll() {
		log.debug("finding all Asset instances");
		try {
			String queryString = "from Asset";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Asset merge(Asset detachedInstance) {
		log.debug("merging Asset instance");
		try {
			Asset result = (Asset) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Asset instance) {
		log.debug("attaching dirty Asset instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Asset instance) {
		log.debug("attaching clean Asset instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static AssetDAOImpl getFromApplicationContext(ApplicationContext ctx) {
		return (AssetDAOImpl) ctx.getBean("AssetDAO");
	}
}